<%-- 
    Document   : attendancedetail
    Created on : 2019-7-19, 9:29:21
    Author     : dusin
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title><link rel="stylesheet" href="../../js/jquery/themes/default/easyui.css" type="text/css" />
        <link rel="stylesheet" href="../../js/jquery/themes/icon.css" type="text/css" />
        <link rel="stylesheet" href="../../js/jquery/themes/color.css" type="text/css" />
        <link rel="stylesheet" href="../../css/demo.css" type="text/css" />
        <script type="text/javascript" src="../../js/jquery/jquery.min.js"></script>
        <script type="text/javascript" src="../../js/jquery/jquery.easyui.min.js"></script>

    </head>
    <body>
        <script type="text/javascript">
            var url;
            var teachclassid = '<%=session.getAttribute("teachclassid")%>'
            var no = '<%=request.getParameter("no")%>';
            var teachclassname = '<%=request.getParameter("text")%>';
            var courseName = '<%=session.getAttribute("courseName")%>'
            var homeworkId = '<%=request.getParameter("homeworkId")%>';

            //alert(teachclassid+teachclassname);
            var teachclassno;
            $(document).ready(function() {
                $('dg-detail').attr('id', 'dg-detail' + no);
                //alert(homeworkId);
            });

            function download1() {
                //alert("ok");
                var row = $('#dg-detail').datagrid('getSelected');

                //alert(row.path);
                if (row) {
                    var filename = row.path;
                    var status = row.status;
                    if (status == 1 || status == 3) {   //已上传
                        var url = "../../servlet/teacher/Download?type=1&no=" + no + "&fileName=" + filename;
                        open(encodeURI(url));

                    } else {
                        $.messager.show({// show error message
                            title: 'Error',
                            msg: '文件尚未上传'
                        });

                    }
                } else {
                    $.messager.show({// show error message
                        title: 'Error',
                        msg: '请先选中一条记录操作'
                    });
                }
            }
            function refreshUser() {
                var rows = $('#dg-detail').datagrid('getChanges');
                if (rows == undefined || rows == null || rows == "") { //没有变化，可以关闭窗口
                    $('#dg-detail').datagrid('reload');    // reload the user data
                } else {
                    $.messager.confirm('提示', '记录已更改，还没有保存，刷新将丢失更改！确定要刷新吗？', function(r) {
                        if (r) {
                            $('#dg-detail').datagrid('reload');    // reload the user data
                        }
                    });
                }
            }

            var editIndex = undefined;
            function endEditing() {
                if (editIndex == undefined) {
                    return true;
                }
                if ($('#dg-detail').datagrid('validateRow', editIndex)) {
                    var ed = $('#dg-detail').datagrid('getEditor', {index: editIndex, field: 'score'});
                    var name = $(ed.target).combobox('getText');
                    $('#dg-detail').datagrid('getRows')[editIndex]['status'] = name;
                    $('#dg-detail').datagrid('endEdit', editIndex);
                    editIndex = undefined;
                    return true;
                } else {
                    return false;
                }
            }
            function onDblClickRow(index) {
                var ed = $('#dg-detail').datagrid('getEditor', {index: index, field: 'status'});
                //$(ed.target).combobox('select',0);     //自动选中第一项，否则会返回""
                if (editIndex != index) {
                    if (endEditing()) {
                        $('#dg-detail').datagrid('selectRow', index)
                                .datagrid('beginEdit', index);
                        editIndex = index;
                    } else {
                        $('#dg-detail').datagrid('selectRow', editIndex);
                    }
                }
            }
            function onClickRow(index) {
                endEditing();
            }

            function accept() {
                if (endEditing()) {
                    $('#dg-detail').datagrid('acceptChanges');
                }
            }
            function reject() {
                $('#dg-detail').datagrid('rejectChanges');
                editIndex = undefined;
            }
            
            //测回，待实现
            function retreat() {
                
            }
            function getChanges() {  //将变化发送到后台保存
                endEditing();   //先结束编辑
                var rows = $('#dg-detail').datagrid('getChanges');
                var data = new Array(rows.length);
                for (var i = 0; i < rows.length; i++) {
                    var rec = {};
                    rec.studentNo = rows[i].studentno;
                    rec.note = rows[i].note;
                    if (rows[i] == null || rows[i] == undefined || rows[i] == "" || rows[i] == 0) {
                        rec.stauts = 0;
                    } else {
                        rec.status = rows[i].status;
                    }
                    data[i] = rec;
                }
                var dataStr = JSON.stringify(data);
                $.ajax({url: "../../servlet/lab/UpdateLabDetail",
                    data: {data: dataStr, no: no, homeworkId: homeworkId},
                    success: function(result) {
                        //var r = eval('(' + result + ')');
                        var r = result;
                        if (r.status){                            
                            $.messager.show({title: '提示', msg:r.msg, timeout: 5000, showType: 'slide'});
                            accept();
                        }else{
                            $.messager.show({title: '提示', msg: r.msg, timeout: 5000, showType: 'slide'});
                            //accept();
                        }                        
                    },
                    error: function(result) {
                        $.messager.show({title: '提示', msg: '实验记录保存失败', timeout: 5000, showType: 'slide'});
                    }
                });
            }
            function formatterAttendanceStatus(value, row) {
                if (value == 100) {   //这里value的类型是String，不能用‘===’三等号
                    return 'A+';
                } else if (value == 95) {
                    return 'A';
                } else if (value == 90) {
                    return 'B+';
                } else if (value == 85) {
                    return 'B';
                } else if (value == 80) {
                    return 'C+';
                } else if (value == 75) {
                    return 'C';
                } else if (value == 70) {
                    return 'D+';
                } else if (value == 65) {
                    return 'D';
                } else if (value == 60) {
                    return 'E';
                } else {
                    return '<span style="color:red;">' + 'F' + '</span>';
                }
            }

        </script>
        <table id="dg-detail" title="${courseName}" class="easyui-datagrid"
               url="../../servlet/lab/GetLabDetail"
               toolbar="#toolbar-detail"
               rownumbers="true" fitColumns="true"
               data-options="iconCls: 'icon-edit',
               singleSelect: true,
               onClickRow:onClickRow,
               height:($(window).height()-40)+'px',
               queryParams:{homeworkId:homeworkId},
               onDblClickRow: onDblClickRow
               ">
            <thead>
                <tr>                    
                    <th field="no" width="50">序号</th>
                    <th field="studentno" width="100">学号</th>
                    <th field="name" width="150">姓名</th>                   
                    <th data-options="field:'score',width:100,align:'center',
                        formatter:formatterAttendanceStatus,
                        editor:{
                        type:'combobox',
                        options:{
                        valueField:'id',
                        textField:'name',
                        method:'get',
                        url:'../data/scoretype.json',
                        required:true
                        }
                        }">分数</th>
                    <th data-options="field:'note',width:150,editor:'textbox'">备注</th> 
                    <th data-options="field:'count',width:50">提交次数</th> 
                    <th data-options="field:'time',width:150">提交时间</th> 
                    <th data-options="field:'path',width:150" >文件名</th> 
                    <th data-options="field:'degree',width:50" >重复度</th> 
                    <th data-options="field:'degreeMatchFile',width:150" >重复文件</th> 
                </tr>
            </thead>
        </table>
        <div id="toolbar-detail">          
            <a href="#" class="easyui-linkbutton" iconCls="icon-reload" plain="true" onclick="refreshUser()">刷新</a>
            <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-save',plain:true" onclick="getChanges()">保存</a>
            <!--<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-save',plain:true" onclick="retreat()">退回</a>-->
            <a href="#" class="easyui-linkbutton" iconCls="icon-reload" plain="true" onclick="download1()">下载</a>
        </div>

        <style type="text/css">
            #fm{
                margin:0;
                padding:10px 30px;
            }
            .ftitle{
                font-size:14px;
                font-weight:bold;
                padding:5px 0;
                margin-bottom:10px;
                border-bottom:1px solid #ccc;
            }
            .fitem{
                margin-bottom:5px;
            }
            .fitem label{
                display:inline-block;
                width:80px;
            }
            .fitem input{
                width:160px;
            }
        </style>
    </body>
</html>
